package com.zhiwang.xiaoqu.mvp.presenter

import android.annotation.SuppressLint
import androidSchedule
import com.zhiwang.xiaoqu.di.annotation.ActivityScope
import com.zhiwang.xiaoqu.mvp.model.*
import com.zhiwang.xiaoqu.mvp.model.request.BindAliAccountRequest
import com.zhiwang.xiaoqu.mvp.model.request.LoginRequest
import com.zhiwang.xiaoqu.mvp.model.request.PageRequest
import com.zhiwang.xiaoqu.mvp.model.request.WithDrawRequest
import com.zhiwang.xiaoqu.mvp.model.response.*
import com.zhiwang.xiaoqu.mvp.model.sharepref.PrefManager
import io.reactivex.functions.BiFunction
import responseFunc
import responseVoidFunc
import javax.inject.Inject


@ActivityScope
class UserPresenter @Inject constructor(var restApi: RestApi) {


    fun loginByPhone(
        phone: String,
        pwd: String,
        deviceId: String,
        onSuccess: (() -> Unit),
        onError: ((Throwable) -> Unit)
    ) {

        val loginRequest = LoginRequest(phone, pwd, deviceId)
        with(restApi) {
            loginByPhone(loginRequest)
                .androidSchedule()
                .responseFunc()
                .subscribe({
                    PrefManager.setToken(it.token)
                    onSuccess()
                }, {
                    onError(it)
                }, {

                })
        }


    }

    fun bindAliAccount(
        phone: String,
        account: String,
        graphCode: String,
        sms: String,
        onBind: () -> Unit,
        onError: ((Throwable) -> Unit)
    ) {

        with(restApi) {
            bindAliAccount(BindAliAccountRequest(phone, graphCode, account, sms))
                .androidSchedule()
                .responseVoidFunc()
                .subscribe({

                }, {
                    onError(it)
                }, {
                    onBind()
                })
        }
    }

    fun getAliWithDrawList(onLoad: ((List<WithDrawItem>) -> Unit), onError: (Throwable) -> Unit) {
        with(restApi) {
            getWithDrawList("alipay")
                .androidSchedule()
                .responseFunc()
                .subscribe({
                    onLoad(it)
                }, {
                    onError(it)
                }, {

                })
        }
    }

    fun getWeiXinWithDrawList(onLoad: (List<WithDrawItem>) -> Unit, onError: (Throwable) -> Unit) {

        with(restApi) {
            getWithDrawList("weixin")
                .androidSchedule()
                .responseFunc()
                .subscribe({
                    onLoad(it)
                }, {
                    onError(it)
                }, {

                })
        }

    }


    fun getMessageList(
        page: Int,
        onRefresh: ((List<Message>) -> Unit),
        onLoadMore: ((List<Message>) -> Unit),
        onError: (Throwable) -> Unit
    ) {
        with(restApi) {
            getMessageList(PageRequest(page))
                .androidSchedule()
                .responseFunc()
                .subscribe({
                    if (it.page == 1)
                        onRefresh(it.dataList)
                    else onLoadMore(it.dataList)
                }, {
                    onError(it)
                }, {

                })
        }
    }


    /**
     * 微信提现
     */
    @SuppressLint("CheckResult")
    fun withdrawWeiXin(num: String, onSuccess: (String) -> Unit, onError: (Throwable) -> Unit) {
        val req = WithDrawRequest(num, "weixin")
        restApi.withDraw(req)
            .androidSchedule()
            .responseFunc()
            .subscribe({
                onSuccess(it)
            }, {
                onError(it)
            }, {

            })

    }

    fun withdrawAli(num: String, onSuccess: (String) -> Unit, onError: (Throwable) -> Unit) {
        val req = WithDrawRequest(num, "alipay")
        with(restApi) {
            withDraw(req)
                .androidSchedule()
                .responseFunc()

                .subscribe({
                    onSuccess(it)
                }, {
                    onError(it)
                }, {

                })
        }

    }

    fun getWithDrawRecordList(page: Int, onLoad: (List<WithDrawRecord>) -> Unit, onError: (Throwable) -> Unit) {

        with(restApi) {
            getWithDrawRecordList(page)
                .androidSchedule()
                .responseFunc()

                .subscribe({
                    onLoad(it.data)
                }, {
                    onError(it)
                }, {

                })
        }

    }

    fun getFriendInfo(onLoad: (List<FriendModel>) -> Unit, onError: (Throwable) -> Unit) {

        with(restApi) {
            getFriendHeaderInfo()
                .androidSchedule()
                .responseFunc()
                .zipWith(restApi.getFriendList(PageRequest(1))
                    .androidSchedule()
                    .responseFunc(),
                    BiFunction<FriendHeaderInfo, ListReponseWrapper<FriendItem>, List<FriendModel>> { t, u ->
                        val list = arrayListOf<FriendModel>()
                        list.add(t)
                        list.addAll(u.dataList)
                        list
                    })

                .subscribe({

                    onLoad(it)
                }, {

                    onError(it)
                }, {

                })
        }

    }

    fun getFriendList(page: Int, onLoad: (List<FriendModel>) -> Unit, onError: (Throwable) -> Unit) {
        with(restApi) {
            getFriendList(PageRequest(page))
                .androidSchedule()
                .responseFunc()
                .subscribe({
                    onLoad(it.dataList)
                }, {
                    onError(it)
                }, {

                })
        }
    }

    fun setMessageRead(onError: (Throwable) -> Unit, onSuccess: () -> Unit) {
        with(restApi) {
            setMessageRead()
                .androidSchedule()
                .responseVoidFunc()
                .subscribe({

                }, {
                    onError(it)
                }, {
                    onSuccess.invoke()
                })
        }
    }

    fun getServiceInfo(onSuccess: (QQServiceInfo) -> Unit, onError: (Throwable) -> Unit) {

        with(restApi) {
            getServiceInfo()
                .androidSchedule()
                .responseFunc()

                .subscribe({
                    onSuccess(it)
                }, {
                    onError(it)
                }, {

                })
        }
    }

    fun getUserInfo(onSuccess: (MyUserInfo) -> Unit, onError: (Throwable) -> Unit) {

        with(restApi) {
            getUserInfo()
                .androidSchedule()
                .responseFunc()

                .subscribe({
                    onSuccess(it)
                }, {
                    onError(it)
                }, {

                })
        }
    }

    fun getAboutInfo() {
        restApi.getAppAbout()
    }

    fun getMoneyDetailList(
        page: Int,
        onLoadSuccess: (ArrayList<MoneyDetailModel>) -> Unit,
        onError: (Throwable) -> Unit
    ) {
        with(restApi) {
            getMoneyDetailList(PageRequest(page))
                .androidSchedule()
                .responseFunc()
                .map {
                    val res = ArrayList<MoneyDetailModel>()

                    var currDate = "1990-01-01"

                    var sum: Int = 0

                    var headerModel: MoneyHeaderModel? = null

                    it.list.forEach {

                        val itemDate = it.date.substring(0, it.date.indexOf(" "))

                        if (itemDate == currDate) {
                            sum += it.number
                            res.add(MoneyItemModel(it.date, it.number, it.cost_text))
                        } else {

                            currDate = itemDate
                            //更新之前所有 赚钱条目 总合
                            if (headerModel != null)
                                headerModel?.sum = sum

                            //先插入一条新的头部的 归总数据；重新计算 tmpSum
                            headerModel = MoneyHeaderModel(0, itemDate)
                            res.add(headerModel!!)

                            //加入本条数据
                            res.add(MoneyItemModel(it.date, it.number, it.cost_text))

                            sum = it.number
                        }
                    }

                    //遍历完成之后 更新最后一条归总数据的金额
                    headerModel?.sum = sum

                    res
                }

                .subscribe({

                    onLoadSuccess(it)

                }, {

                    onError(it)
                }, {

                })
        }
    }

    fun checkWxBind(code: String, deviceId: String) {

        val subscribe = restApi.checkWxBind(code, deviceId)
            .androidSchedule()
            .responseFunc()
            .subscribe({
                //                checkBindWxView.onCheckWxBind(it)
            }, {
                //                checkBindWxView.onError(it)
            }, {
                //                Log.i(TAG, "checkWxBind: ")
            })

    }

    fun getIndexInfo(onLoadSuccess: (IndexUserInfo) -> Unit, onError: (Throwable) -> Unit) {
        with(restApi) {
            getIndexData()
                .androidSchedule()
                .responseFunc()
                .subscribe({
                    onLoadSuccess(it)
                }, {
                    onError(it)
                }, {

                })
        }
    }


}